<template>
  <div>
    <header class="bookxq">
      <div class="title">
        <router-link to="/home" class="left">
          <span class="ui-leftaw-2"></span>
          <!-- <span class="ui-leftaw-2"></span> -->
        </router-link>
        <div class="tit-top">
          <h3>用户页</h3>
        </div>
      </div>
    </header>
    <div class="userdata">
      <div class="img-box">
        <div class="userimg">
          <div class="img" @click="getimg">
            <img :src="user.src" alt="" />
          </div>
        </div>
        <div class="username">
          <h2 @click="getimg">{{ user.name }}</h2>
        </div>
        <div class="namechange" v-show="namec">
          <input v-model="name" type="text" placeholder="请输入正确的名字"    @keydown.enter="namechages"/>
        </div>
        <div class="lookdata">
          <router-link to="/shelf" class="bookdata">
            <i>{{ user.bookid }}</i>
            <p>收藏</p>
          </router-link>
          <router-link to="/cookic" class="bookdata">
            <i>{{ user.cookic }}</i>
            <p>历史</p>
          </router-link>
          <!-- <div><i>30</i><p>书架</p></div> -->
        </div>
      </div>
    </div>
    <div class="imgchage" v-show="imgchage">
      <div class="imglist" @click="imgchages">
        <ul style="display: flex; flex-wrap: wrap">
          <li><img src="../static/usersrc/dog.png" alt="" /></li>
          <li><img src="../static/usersrc/dogs.png" alt="" /></li>
          <li><img src="../static/usersrc/dogss.png" alt="" /></li>
          <li><img src="../static/usersrc/icon_search.png" alt="" /></li>
          <li><img src="../static/usersrc/icon_user.png" alt="" /></li>
          <li><img src="../static/usersrc/loading.gif" alt="" /></li>
          <li><img src="../static/usersrc/logo.png" alt="" /></li>
          <li><img src="../static/usersrc/user1.png" alt="" /></li>
        </ul>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      user: {
        name: "",
        src: "",
        bookid: 0,
        cookic: 0,
      },
      imgchage: false,
      namec: false,
      name: "",
    };
  },
  methods: {
    getuser() {
      let datastr = localStorage.getItem("userdata");
      if (datastr) {
        let iddata = JSON.parse(datastr);
        // console.log(iddata);
        let userdata = iddata.user;
        this.user.name = userdata.name;
        this.user.src = userdata.src;
        let bookdatas = iddata.bookid;
        let cookics = iddata.cookic;
        if (bookdatas.length >= 0) {
          this.user.bookid = bookdatas.length;
        }
        if (cookics.length >= 0) {
          this.user.cookic = cookics.length;
        }
        console.log(bookdatas.length);
      }
    },
    getimg() {
      this.imgchage = !this.imgchage;
    },
    imgchages(e) {
      e = e || window.event;
      if (this.imgchage == true) {
        // console.log(e.target);
        if (e.target.tagName === "IMG") {
          let datastr = localStorage.getItem("userdata");
          if (datastr) {
            let iddata = JSON.parse(datastr);
            // console.log(e.target.src);
            iddata.user.src = e.target.src;
            // console.log(iddata);
            this.user.src = iddata.src;
            localStorage.setItem("userdata", JSON.stringify(iddata));
            alert("头像更换成功");
            this.$router.go(0);
          }
        }
      }
    },
    getimg() {
      this.namec = !this.namec;
    },
    namechages() {
      if (this.namec == true) {
        // console.log(e.target);
        if (this.name == "") {
          alert("输入框不能为空");
          return;
        } else {
          let datastr = localStorage.getItem("userdata");
          if (datastr) {
            let iddata = JSON.parse(datastr);
            let resname=/\w{3,}/
            if (resname.test(this.name) !== true) {
              alert("请正确输入用户名，用户名格式为任意三位及以上字母或数字");
              return;
            }
            iddata.user.name =this.name
            // console.log(iddata);
            this.user.name = iddata.name;
            localStorage.setItem("userdata", JSON.stringify(iddata));
            alert("用户名称更换成功");
            this.$router.go(0);
          }
        }
      }
    },
  },
  // 初始化函数
  created() {
    this.getuser();
  },
  beforeRouteEnter(to, from, next) {
    // 访问详情之前做一层判断
    let token = localStorage.getItem("userdata");
    // 判断token是否有值  null 表示没有登录
    if (token !== null) {
      // 用户已经登录就可以访问详情页
      next();
    } else {
      // 否则跳转登录页
      // alert("请先登录");
      next({ name: "login" });
    }
  },
};
</script>
  <style lang="scss">
@import url(../static/css/reset.css);
.bookxq {
  width: 100%;
  background-color: black;
  min-width: 362px;
  .title {
    margin: auto 1%;
    height: 44px;
    line-height: 44px;
    a.left {
      float: left;
      font-size: 14px;
      .ui-leftaw-2 {
        display: inline-block;
        margin-bottom: 3px;
        margin-left: 15px;
        vertical-align: middle;
        width: 10px;
        height: 18px;
        background: url(../static/imgs/arrow-left.png);
        background-size: 100% 100%;
      }
    }
    .tit-top {
      padding: 0 5%;
      box-sizing: border-box;
      text-align: center;
      h3 {
        width: 30%;
        display: inline-block;
        color: white;
      }
    }
  }
}
.userdata {
  background: #000;
  box-sizing: content-box;
  padding: 10px 10px;
  .img-box {
    background-color: #fff;
    .userimg {
      padding: 10px;
      .img {
        width: 100px;
        height: 100px;
        margin: 0 auto;
        img {
          display: block;
          width: 100px;
          height: 100px;
          border: 1px solid black;
          border-radius: 50%;
        }
      }
      //   background-color: darkblue;
    }
    .username {
      display: block;
      margin: 10px auto;
      width: 100px;
      height: 30px;
      line-height: 30px;
      //   background: #930;
      text-align: center;
    }
    .namechange {
      width: 100%;
      height: 30px;
      line-height: 30px;
      // background: #341a1a;
      input {
        display: block;
        margin: 0 auto;
        color: #fff;
        width: 30%;
        height: 30px;
        line-height: 30px;
        background: #ed0707;
      }
    }
    .lookdata {
      width: 100%;
      height: 50px;
      display: flex;
      //   background: rgb(213, 196, 196);
      .bookdata {
        border: 1px solid black;
        text-align: center;
        width: 50%;
        height: 100%;
      }
    }
  }
}
.imgchage {
  position: fixed;
  background: #000;
  padding: 11px;
  .imglist {
    background-color: #fff;
    ul {
      li {
        width: 25%;
        height: 100%;
        img {
          width: 100%;
          height: 100%;
        }
      }
    }
  }
}
</style>